
global ZbaseD = "k"
global INSD = "dm_lag_q_s dm_k"

clear all
use "$working/prowess_wits_estimation_demand", clear

xtset product_id year

foreach v in q_s ln_price ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k k_alt ln_wIPT_IND_CHN  {
	gen lag_`v' = L.`v'
}
			
foreach v in q_s ln_price ln_IND ln_IVS ln_wIPT_IND_CHN  {
	drop if lag_`v'==. | `v'==. 
}
			
foreach v in  $ZbaseD {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
foreach v in q_s ln_price lag_q_s lag_ln_price lag_k lag_ln_IND lag_ln_IVS lag_ln_wIPT_IND_CHN  $ZbaseD {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
						
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}

						
gmm (dm_q_s - {rho=0.9}*dm_lag_q_s - {sigma=-2}*(dm_ln_price) + {rho}*{sigma}*(dm_lag_ln_price) ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN }), ///
	instruments($INSD dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
	
predict resid, residuals 

gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]
		
gen sigma_hat = ([sigma]_cons)
gen sigma_hat_se = _se[sigma:_cons]

gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]

xtset product_id year
generate lag_resid = L.resid
reg resid lag_resid, cluster(co_code1)

generate perc_test = _b[lag_resid]
generate se_perc_test = _se[lag_resid]
			
replace nic_08_2dig = "all"
contract nic_08_2dig rho_hat rho_hat_se sigma_hat sigma_hat_se beta_lag_ln_IND beta_lag_ln_IND_se beta_lag_MA beta_lag_MA_se ///
 perc_test se_perc_test
		
rename _freq Observations

save "$working/full_ProdD_L1", replace


global ZbaseD = "k"
global INSD = "dm_lag_q_s dm_k dm_lag_k"


clear all
use "$working/prowess_wits_estimation_demand", clear

xtset product_id year

foreach v in q_s ln_price ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k k_alt ln_wIPT_IND_CHN  {
	gen lag_`v' = L.`v'
}
			
foreach v in q_s ln_price ln_IND ln_IVS ln_wIPT_IND_CHN  {
	drop if lag_`v'==. | `v'==. 
}
			
foreach v in  $ZbaseD {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
foreach v in q_s ln_price lag_q_s lag_ln_price lag_k lag_ln_IND lag_ln_IVS lag_ln_wIPT_IND_CHN  $ZbaseD {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
						
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}

						
gmm (dm_q_s - {rho=0.9}*dm_lag_q_s - {sigma=-2}*(dm_ln_price) + {rho}*{sigma}*(dm_lag_ln_price) ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN }), ///
	instruments($INSD dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
	
predict resid, residuals 

gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]
		
gen sigma_hat = ([sigma]_cons)
gen sigma_hat_se = _se[sigma:_cons]

gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]

xtset product_id year
generate lag_resid = L.resid
reg resid lag_resid, cluster(co_code1)

generate perc_test = _b[lag_resid]
generate se_perc_test = _se[lag_resid]
			
replace nic_08_2dig = "all"
contract nic_08_2dig rho_hat rho_hat_se sigma_hat sigma_hat_se beta_lag_ln_IND beta_lag_ln_IND_se beta_lag_MA beta_lag_MA_se ///
 perc_test se_perc_test
		
rename _freq Observations

save "$working/full_ProdD_L1_alt", replace


global ZbaseD = "k"
global INSD = "dm_lag_q_s dm_lag2_q_s dm_k dm_lag_k dm_lag2_k"


clear all
use "$working/prowess_wits_estimation_demand", clear


xtset product_id year

foreach v in q_s ln_price ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k k_alt ln_wIPT_IND_CHN  {
	gen lag_`v' = L.`v'
	gen lag2_`v' = L2.`v'
}
			
foreach v in q_s ln_price ln_IND ln_IVS ln_wIPT_IND_CHN  {
	drop if lag_`v'==. | `v'==. | lag2_`v'==.  
}
			
foreach v in  $ZbaseD {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
foreach v in q_s ln_price lag_q_s lag_ln_price lag_k lag_ln_IND lag_ln_IVS lag_ln_wIPT_IND_CHN  $ZbaseD ///
lag2_q_s lag2_k lag2_ln_price lag2_ln_IND lag2_ln_IVS lag2_ln_wIPT_IND_CHN {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
						
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}

		
gmm (dm_q_s - {rho=0.9}*dm_lag_q_s - {rho2=-0.2}*dm_lag2_q_s  - {sigma=-2}*(dm_ln_price) + {rho}*{sigma}*(dm_lag_ln_price) + {rho2}*{sigma}*(dm_lag2_ln_price) ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN  dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN}), ///
	instruments($INSD dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
	
predict resid, residuals 

gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]

gen rho2_hat = [rho2]_cons
gen rho2_hat_se = _se[rho2:_cons]

gen sigma_hat = ([sigma]_cons)
gen sigma_hat_se = _se[sigma:_cons]

gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]


gen beta_lag2_ln_IND = _b[xb:dm_lag2_ln_IND]
gen beta_lag2_ln_IND_se = _se[xb: dm_lag2_ln_IND]

gen beta_lag2_MA = _b[xb: dm_lag2_ln_wIPT_IND_CHN]
gen beta_lag2_MA_se = _se[xb: dm_lag2_ln_wIPT_IND_CHN]


xtset product_id year
generate lag_resid = L.resid
reg resid lag_resid, cluster(co_code1)

generate perc_test = _b[lag_resid]
generate se_perc_test = _se[lag_resid]
			
replace nic_08_2dig = "all"
contract nic_08_2dig rho_hat rho_hat_se sigma_hat sigma_hat_se beta_lag_ln_IND beta_lag_ln_IND_se beta_lag_MA beta_lag_MA_se ///
 perc_test se_perc_test ///
  rho2_hat rho2_hat_se  ///
  beta_lag2_ln_IND beta_lag2_ln_IND_se beta_lag2_MA beta_lag2_MA_se 
		
rename _freq Observations

save "$working/full_ProdD_L2", replace


global ZbaseD = "k"
global INSD = "dm_lag_q_s dm_lag2_q_s dm_lag3_q_s dm_k dm_lag_k dm_lag2_k dm_lag3_k"


clear all
use "$working/prowess_wits_estimation_demand", clear


xtset product_id year

foreach v in q_s ln_price ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k k_alt ln_wIPT_IND_CHN  {
	gen lag_`v' = L.`v'
	gen lag2_`v' = L2.`v'
	gen lag3_`v' = L3.`v'
}
			
foreach v in q_s ln_price ln_IND ln_IVS ln_wIPT_IND_CHN  {
	drop if lag_`v'==. | `v'==. | lag2_`v'==. | lag3_`v'==. 
}
			
foreach v in  $ZbaseD {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
foreach v in q_s ln_price lag_q_s lag_ln_price lag_k lag_ln_IND lag_ln_IVS lag_ln_wIPT_IND_CHN  $ZbaseD ///
lag2_q_s lag2_k lag2_ln_price lag2_ln_IND lag2_ln_IVS lag2_ln_wIPT_IND_CHN ///
lag3_q_s lag3_k lag3_ln_price lag3_ln_IND lag3_ln_IVS lag3_ln_wIPT_IND_CHN {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
						
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}

						
gmm (dm_q_s - {rho=0.9}*dm_lag_q_s - {rho2=-0.2}*dm_lag2_q_s - {rho3=-0.2}*dm_lag3_q_s  - {sigma=-2}*(dm_ln_price) + {rho}*{sigma}*(dm_lag_ln_price) + {rho2}*{sigma}*(dm_lag2_ln_price) + {rho3}*{sigma}*(dm_lag3_ln_price) ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN  dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN dm_lag3_ln_IND dm_lag3_ln_wIPT_IND_CHN}), ///
	instruments($INSD dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN dm_lag3_ln_IND dm_lag3_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
	
predict resid, residuals 

gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]

gen rho2_hat = [rho2]_cons
gen rho2_hat_se = _se[rho2:_cons]

gen rho3_hat = [rho3]_cons
gen rho3_hat_se = _se[rho3:_cons]

gen sigma_hat = ([sigma]_cons)
gen sigma_hat_se = _se[sigma:_cons]

gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]


gen beta_lag2_ln_IND = _b[xb:dm_lag2_ln_IND]
gen beta_lag2_ln_IND_se = _se[xb: dm_lag2_ln_IND]

gen beta_lag2_MA = _b[xb: dm_lag2_ln_wIPT_IND_CHN]
gen beta_lag2_MA_se = _se[xb: dm_lag2_ln_wIPT_IND_CHN]


gen beta_lag3_ln_IND = _b[xb:dm_lag3_ln_IND]
gen beta_lag3_ln_IND_se = _se[xb: dm_lag3_ln_IND]

gen beta_lag3_MA = _b[xb: dm_lag3_ln_wIPT_IND_CHN]
gen beta_lag3_MA_se = _se[xb: dm_lag3_ln_wIPT_IND_CHN]

xtset product_id year
generate lag_resid = L.resid
reg resid lag_resid, cluster(co_code1)

generate perc_test = _b[lag_resid]
generate se_perc_test = _se[lag_resid]
			
replace nic_08_2dig = "all"
contract nic_08_2dig rho_hat rho_hat_se sigma_hat sigma_hat_se beta_lag_ln_IND beta_lag_ln_IND_se beta_lag_MA beta_lag_MA_se ///
 perc_test se_perc_test ///
  rho2_hat rho2_hat_se  rho3_hat rho3_hat_se ///
  beta_lag2_ln_IND beta_lag2_ln_IND_se beta_lag2_MA beta_lag2_MA_se ///
  beta_lag3_ln_IND beta_lag3_ln_IND_se beta_lag3_MA beta_lag3_MA_se 
		
rename _freq Observations

save "$working/full_ProdD_L3", replace


global ZbaseD = "k"
global INSD = "dm_lag_q_s dm_lag2_q_s dm_lag3_q_s dm_lag4_q_s dm_k dm_lag_k dm_lag2_k dm_lag3_k dm_lag4_k"


clear all
use "$working/prowess_wits_estimation_demand", clear


xtset product_id year

foreach v in q_s ln_price ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k k_alt ln_wIPT_IND_CHN  {
	gen lag_`v' = L.`v'
	gen lag2_`v' = L2.`v'
	gen lag3_`v' = L3.`v'
	gen lag4_`v' = L4.`v'
}
			
foreach v in q_s ln_price ln_IND ln_IVS ln_wIPT_IND_CHN  {
	drop if lag_`v'==. | `v'==. | lag2_`v'==. | lag3_`v'==. | lag4_`v'==.
}
			
foreach v in  $ZbaseD {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
foreach v in q_s ln_price lag_q_s lag_ln_price lag_k lag_ln_IND lag_ln_IVS lag_ln_wIPT_IND_CHN  $ZbaseD ///
lag2_q_s lag2_k lag2_ln_price lag2_ln_IND lag2_ln_IVS lag2_ln_wIPT_IND_CHN ///
lag3_q_s lag3_k lag3_ln_price lag3_ln_IND lag3_ln_IVS lag3_ln_wIPT_IND_CHN ///
lag4_q_s lag4_k lag4_ln_price lag4_ln_IND lag4_ln_IVS lag4_ln_wIPT_IND_CHN {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
						
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}

						
gmm (dm_q_s - {rho=0.9}*dm_lag_q_s - {rho2=-0.2}*dm_lag2_q_s - {rho3=-0.2}*dm_lag3_q_s  - {rho4=-0.2}*dm_lag4_q_s - {sigma=-2}*(dm_ln_price) + {rho}*{sigma}*(dm_lag_ln_price) + {rho2}*{sigma}*(dm_lag2_ln_price) + {rho3}*{sigma}*(dm_lag3_ln_price) + {rho4}*{sigma}*(dm_lag4_ln_price) ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN  dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN dm_lag3_ln_IND dm_lag3_ln_wIPT_IND_CHN dm_lag4_ln_IND dm_lag4_ln_wIPT_IND_CHN}), ///
	instruments($INSD dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN dm_lag3_ln_IND dm_lag3_ln_wIPT_IND_CHN  dm_lag4_ln_IND dm_lag4_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
	
predict resid, residuals 

gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]

gen rho2_hat = [rho2]_cons
gen rho2_hat_se = _se[rho2:_cons]

gen rho3_hat = [rho3]_cons
gen rho3_hat_se = _se[rho3:_cons]

gen rho4_hat = [rho4]_cons
gen rho4_hat_se = _se[rho4:_cons]
		
gen sigma_hat = ([sigma]_cons)
gen sigma_hat_se = _se[sigma:_cons]

gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]


gen beta_lag2_ln_IND = _b[xb:dm_lag2_ln_IND]
gen beta_lag2_ln_IND_se = _se[xb: dm_lag2_ln_IND]

gen beta_lag2_MA = _b[xb: dm_lag2_ln_wIPT_IND_CHN]
gen beta_lag2_MA_se = _se[xb: dm_lag2_ln_wIPT_IND_CHN]


gen beta_lag3_ln_IND = _b[xb:dm_lag3_ln_IND]
gen beta_lag3_ln_IND_se = _se[xb: dm_lag3_ln_IND]

gen beta_lag3_MA = _b[xb: dm_lag3_ln_wIPT_IND_CHN]
gen beta_lag3_MA_se = _se[xb: dm_lag3_ln_wIPT_IND_CHN]


gen beta_lag4_ln_IND = _b[xb:dm_lag4_ln_IND]
gen beta_lag4_ln_IND_se = _se[xb: dm_lag4_ln_IND]

gen beta_lag4_MA = _b[xb: dm_lag4_ln_wIPT_IND_CHN]
gen beta_lag4_MA_se = _se[xb: dm_lag4_ln_wIPT_IND_CHN]


xtset product_id year
generate lag_resid = L.resid
reg resid lag_resid, cluster(co_code1)

generate perc_test = _b[lag_resid]
generate se_perc_test = _se[lag_resid]
			
replace nic_08_2dig = "all"
contract nic_08_2dig rho_hat rho_hat_se sigma_hat sigma_hat_se beta_lag_ln_IND beta_lag_ln_IND_se beta_lag_MA beta_lag_MA_se ///
 perc_test se_perc_test ///
  rho2_hat rho2_hat_se  rho3_hat rho3_hat_se  rho4_hat rho4_hat_se ///
  beta_lag2_ln_IND beta_lag2_ln_IND_se beta_lag2_MA beta_lag2_MA_se ///
  beta_lag3_ln_IND beta_lag3_ln_IND_se beta_lag3_MA beta_lag3_MA_se ///
  beta_lag4_ln_IND beta_lag4_ln_IND_se beta_lag4_MA beta_lag4_MA_se 
		
rename _freq Observations

save "$working/full_ProdD_L4", replace


global ZbaseD = "k"
global INSD = "dm_lag_q_s dm_lag2_q_s dm_lag3_q_s dm_lag4_q_s dm_lag5_q_s dm_k dm_lag_k dm_lag2_k dm_lag3_k dm_lag4_k dm_lag5_k"


clear all
use "$working/prowess_wits_estimation_demand", clear


xtset product_id year

foreach v in q_s ln_price ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k k_alt ln_wIPT_IND_CHN  {
	gen lag_`v' = L.`v'
	gen lag2_`v' = L2.`v'
	gen lag3_`v' = L3.`v'
	gen lag4_`v' = L4.`v'
	gen lag5_`v' = L5.`v'
}
			
foreach v in q_s ln_price ln_IND ln_IVS ln_wIPT_IND_CHN  {
	drop if lag_`v'==. | `v'==. | lag2_`v'==. | lag3_`v'==. | lag4_`v'==. | lag5_`v'==. 
}
			
foreach v in  $ZbaseD {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
foreach v in q_s ln_price lag_q_s lag_ln_price lag_k lag_ln_IND lag_ln_IVS lag_ln_wIPT_IND_CHN  $ZbaseD ///
lag2_q_s lag2_k lag2_ln_price lag2_ln_IND lag2_ln_IVS lag2_ln_wIPT_IND_CHN ///
lag3_q_s lag3_k lag3_ln_price lag3_ln_IND lag3_ln_IVS lag3_ln_wIPT_IND_CHN ///
lag4_q_s lag4_k lag4_ln_price lag4_ln_IND lag4_ln_IVS lag4_ln_wIPT_IND_CHN ///
lag5_q_s lag5_k lag5_ln_price lag5_ln_IND lag5_ln_IVS lag5_ln_wIPT_IND_CHN {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
						
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}

						
gmm (dm_q_s - {rho=0.9}*dm_lag_q_s - {rho2=-0.2}*dm_lag2_q_s - {rho3=-0.2}*dm_lag3_q_s  - {rho4=-0.2}*dm_lag4_q_s  - {rho5=-0.2}*dm_lag5_q_s - {sigma=-2}*(dm_ln_price) + {rho}*{sigma}*(dm_lag_ln_price) + {rho2}*{sigma}*(dm_lag2_ln_price) + {rho3}*{sigma}*(dm_lag3_ln_price) + {rho4}*{sigma}*(dm_lag4_ln_price) + {rho5}*{sigma}*(dm_lag5_ln_price) ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN  dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN dm_lag3_ln_IND dm_lag3_ln_wIPT_IND_CHN dm_lag4_ln_IND dm_lag4_ln_wIPT_IND_CHN dm_lag5_ln_IND dm_lag5_ln_wIPT_IND_CHN}), ///
	instruments($INSD dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN dm_lag3_ln_IND dm_lag3_ln_wIPT_IND_CHN  dm_lag4_ln_IND dm_lag4_ln_wIPT_IND_CHN dm_lag5_ln_IND dm_lag5_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
	
predict resid, residuals 

gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]

gen rho2_hat = [rho2]_cons
gen rho2_hat_se = _se[rho2:_cons]

gen rho3_hat = [rho3]_cons
gen rho3_hat_se = _se[rho3:_cons]

gen rho4_hat = [rho4]_cons
gen rho4_hat_se = _se[rho4:_cons]

gen rho5_hat = [rho5]_cons
gen rho5_hat_se = _se[rho5:_cons]
		
gen sigma_hat = ([sigma]_cons)
gen sigma_hat_se = _se[sigma:_cons]

gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]


gen beta_lag2_ln_IND = _b[xb:dm_lag2_ln_IND]
gen beta_lag2_ln_IND_se = _se[xb: dm_lag2_ln_IND]

gen beta_lag2_MA = _b[xb: dm_lag2_ln_wIPT_IND_CHN]
gen beta_lag2_MA_se = _se[xb: dm_lag2_ln_wIPT_IND_CHN]


gen beta_lag3_ln_IND = _b[xb:dm_lag3_ln_IND]
gen beta_lag3_ln_IND_se = _se[xb: dm_lag3_ln_IND]

gen beta_lag3_MA = _b[xb: dm_lag3_ln_wIPT_IND_CHN]
gen beta_lag3_MA_se = _se[xb: dm_lag3_ln_wIPT_IND_CHN]


gen beta_lag4_ln_IND = _b[xb:dm_lag4_ln_IND]
gen beta_lag4_ln_IND_se = _se[xb: dm_lag4_ln_IND]

gen beta_lag4_MA = _b[xb: dm_lag4_ln_wIPT_IND_CHN]
gen beta_lag4_MA_se = _se[xb: dm_lag4_ln_wIPT_IND_CHN]


gen beta_lag5_ln_IND = _b[xb:dm_lag5_ln_IND]
gen beta_lag5_ln_IND_se = _se[xb: dm_lag5_ln_IND]

gen beta_lag5_MA = _b[xb: dm_lag5_ln_wIPT_IND_CHN]
gen beta_lag5_MA_se = _se[xb: dm_lag5_ln_wIPT_IND_CHN]


xtset product_id year
generate lag_resid = L.resid
reg resid lag_resid, cluster(co_code1)

generate perc_test = _b[lag_resid]
generate se_perc_test = _se[lag_resid]
			
replace nic_08_2dig = "all"
contract nic_08_2dig rho_hat rho_hat_se sigma_hat sigma_hat_se beta_lag_ln_IND beta_lag_ln_IND_se beta_lag_MA beta_lag_MA_se ///
 perc_test se_perc_test ///
  rho2_hat rho2_hat_se  rho3_hat rho3_hat_se  rho4_hat rho4_hat_se  rho5_hat rho5_hat_se ///
  beta_lag2_ln_IND beta_lag2_ln_IND_se beta_lag2_MA beta_lag2_MA_se ///
  beta_lag3_ln_IND beta_lag3_ln_IND_se beta_lag3_MA beta_lag3_MA_se ///
  beta_lag4_ln_IND beta_lag4_ln_IND_se beta_lag4_MA beta_lag4_MA_se ///
  beta_lag5_ln_IND beta_lag5_ln_IND_se beta_lag5_MA beta_lag5_MA_se 
		
rename _freq Observations

save "$working/full_ProdD_L5", replace


global ZbaseD = "k"
global INSD = "dm_lag_q_s dm_lag2_q_s dm_lag3_q_s dm_lag4_q_s dm_lag5_q_s dm_lag6_q_s dm_k dm_lag_k dm_lag2_k dm_lag3_k dm_lag4_k dm_lag5_k dm_lag6_k"


clear all
use "$working/prowess_wits_estimation_demand", clear


xtset product_id year

foreach v in q_s ln_price ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k k_alt ln_wIPT_IND_CHN  {
	gen lag_`v' = L.`v'
	gen lag2_`v' = L2.`v'
	gen lag3_`v' = L3.`v'
	gen lag4_`v' = L4.`v'
	gen lag5_`v' = L5.`v'
	gen lag6_`v' = L6.`v'
}
			
foreach v in q_s ln_price ln_IND ln_IVS ln_wIPT_IND_CHN  {
	drop if lag_`v'==. | `v'==. | lag2_`v'==. | lag3_`v'==. | lag4_`v'==. | lag5_`v'==.  | lag6_`v'==.
}
			
foreach v in  $ZbaseD {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
foreach v in q_s ln_price lag_q_s lag_ln_price lag_k lag_ln_IND lag_ln_IVS lag_ln_wIPT_IND_CHN  $ZbaseD ///
lag2_q_s lag2_k lag2_ln_price lag2_ln_IND lag2_ln_IVS lag2_ln_wIPT_IND_CHN ///
lag3_q_s lag3_k lag3_ln_price lag3_ln_IND lag3_ln_IVS lag3_ln_wIPT_IND_CHN ///
lag4_q_s lag4_k lag4_ln_price lag4_ln_IND lag4_ln_IVS lag4_ln_wIPT_IND_CHN ///
lag5_q_s lag5_k lag5_ln_price lag5_ln_IND lag5_ln_IVS lag5_ln_wIPT_IND_CHN ///
lag6_q_s lag6_k lag6_ln_price lag6_ln_IND lag6_ln_IVS lag6_ln_wIPT_IND_CHN {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
						
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}

						
gmm (dm_q_s - {rho=0.9}*dm_lag_q_s - {rho2=-0.2}*dm_lag2_q_s - {rho3=-0.2}*dm_lag3_q_s  - {rho4=-0.2}*dm_lag4_q_s  - {rho5=-0.2}*dm_lag5_q_s  - {rho6=-0.2}*dm_lag6_q_s - {sigma=-2}*(dm_ln_price) + {rho}*{sigma}*(dm_lag_ln_price) + {rho2}*{sigma}*(dm_lag2_ln_price) + {rho3}*{sigma}*(dm_lag3_ln_price) + {rho4}*{sigma}*(dm_lag4_ln_price) + {rho5}*{sigma}*(dm_lag5_ln_price) + {rho6}*{sigma}*(dm_lag6_ln_price) ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN  dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN dm_lag3_ln_IND dm_lag3_ln_wIPT_IND_CHN dm_lag4_ln_IND dm_lag4_ln_wIPT_IND_CHN dm_lag5_ln_IND dm_lag5_ln_wIPT_IND_CHN dm_lag6_ln_IND dm_lag6_ln_wIPT_IND_CHN}), ///
	instruments($INSD dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN dm_lag2_ln_IND dm_lag2_ln_wIPT_IND_CHN dm_lag3_ln_IND dm_lag3_ln_wIPT_IND_CHN  dm_lag4_ln_IND dm_lag4_ln_wIPT_IND_CHN dm_lag5_ln_IND dm_lag5_ln_wIPT_IND_CHN dm_lag6_ln_IND dm_lag6_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
	
predict resid, residuals 

gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]


gen rho2_hat = [rho2]_cons
gen rho2_hat_se = _se[rho2:_cons]

gen rho3_hat = [rho3]_cons
gen rho3_hat_se = _se[rho3:_cons]

gen rho4_hat = [rho4]_cons
gen rho4_hat_se = _se[rho4:_cons]

gen rho5_hat = [rho5]_cons
gen rho5_hat_se = _se[rho5:_cons]

gen rho6_hat = [rho6]_cons
gen rho6_hat_se = _se[rho6:_cons]
		
gen sigma_hat = ([sigma]_cons)
gen sigma_hat_se = _se[sigma:_cons]

gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]


gen beta_lag2_ln_IND = _b[xb:dm_lag2_ln_IND]
gen beta_lag2_ln_IND_se = _se[xb: dm_lag2_ln_IND]

gen beta_lag2_MA = _b[xb: dm_lag2_ln_wIPT_IND_CHN]
gen beta_lag2_MA_se = _se[xb: dm_lag2_ln_wIPT_IND_CHN]


gen beta_lag3_ln_IND = _b[xb:dm_lag3_ln_IND]
gen beta_lag3_ln_IND_se = _se[xb: dm_lag3_ln_IND]

gen beta_lag3_MA = _b[xb: dm_lag3_ln_wIPT_IND_CHN]
gen beta_lag3_MA_se = _se[xb: dm_lag3_ln_wIPT_IND_CHN]


gen beta_lag4_ln_IND = _b[xb:dm_lag4_ln_IND]
gen beta_lag4_ln_IND_se = _se[xb: dm_lag4_ln_IND]

gen beta_lag4_MA = _b[xb: dm_lag4_ln_wIPT_IND_CHN]
gen beta_lag4_MA_se = _se[xb: dm_lag4_ln_wIPT_IND_CHN]


gen beta_lag5_ln_IND = _b[xb:dm_lag5_ln_IND]
gen beta_lag5_ln_IND_se = _se[xb: dm_lag5_ln_IND]

gen beta_lag5_MA = _b[xb: dm_lag5_ln_wIPT_IND_CHN]
gen beta_lag5_MA_se = _se[xb: dm_lag5_ln_wIPT_IND_CHN]


gen beta_lag6_ln_IND = _b[xb:dm_lag6_ln_IND]
gen beta_lag6_ln_IND_se = _se[xb: dm_lag6_ln_IND]

gen beta_lag6_MA = _b[xb: dm_lag6_ln_wIPT_IND_CHN]
gen beta_lag6_MA_se = _se[xb: dm_lag6_ln_wIPT_IND_CHN]


xtset product_id year
generate lag_resid = L.resid
reg resid lag_resid, cluster(co_code1)

generate perc_test = _b[lag_resid]
generate se_perc_test = _se[lag_resid]
			
replace nic_08_2dig = "all"
contract nic_08_2dig rho_hat rho_hat_se sigma_hat sigma_hat_se beta_lag_ln_IND beta_lag_ln_IND_se beta_lag_MA beta_lag_MA_se ///
 perc_test se_perc_test ///
  rho2_hat rho2_hat_se  rho3_hat rho3_hat_se  rho4_hat rho4_hat_se  rho5_hat rho5_hat_se  rho6_hat rho6_hat_se ///
  beta_lag2_ln_IND beta_lag2_ln_IND_se beta_lag2_MA beta_lag2_MA_se ///
  beta_lag3_ln_IND beta_lag3_ln_IND_se beta_lag3_MA beta_lag3_MA_se ///
  beta_lag4_ln_IND beta_lag4_ln_IND_se beta_lag4_MA beta_lag4_MA_se ///
  beta_lag5_ln_IND beta_lag5_ln_IND_se beta_lag5_MA beta_lag5_MA_se ///
  beta_lag6_ln_IND beta_lag6_ln_IND_se beta_lag6_MA beta_lag6_MA_se 
		
rename _freq Observations

save "$working/full_ProdD_L6", replace
